{% extends "module/common.html" %}
	{% block handle %}
    	var handle={
    		name:'项目',
    		serv:'project'
    	};
    	var relaForm=$("<div style='overflow:auto'><div class='owner_form'><table class='win_table'><thead><tr><td dateKey='remark'>名称</td><td dateKey='ctrl' bindfunc='setOpt' class='last_child'>操作</td></tr></thead><tbody></tbody></table></div>"
        			+"<div class='form_list' callFunc='click:addForm' dataKey='remark'></div>"	
        			+"</div><div align=center style='margin-top:10px'><button class='btn' callFunc='click:saveRela' style='margin-right:5px;'>保存</button><button class='btn' callFunc='click:syncJs'>同步脚本</button></div>");
    	
    	var beanForm=$("<div style='overflow:auto'><div class='owner_form'><table class='win_table'><thead><tr><td dateKey='name'>名称</td><td dateKey='ctrl' bindfunc='setBeanOpt' class='last_child'>操作</td></tr></thead><tbody></tbody></table></div>"
        			+"<div class='form_list' callFunc='click:addBean' dataKey='name'></div>"	
        			+"</div><div align=center style='margin-top:10px'><button class='btn' callFunc='click:createBean' t='batch' style='margin-right:5px;'>批量同步</button><button class='btn' callFunc='click:saveBean'>保存</button></div>");
    	
    	var textForm=$("<div style='overflow:auto'><div class='owner_form' style='height:400px;width:500px;'><table class='win_table'><thead><tr>"+
    					"<td dateKey='table_name'>名称</td>"+
    					"<td dateKey='ctrl' bindfunc='setTextOpt' class='last_child'>操作</td>"+
    					"</tr></thead><tbody></tbody></table></div></div>");
    	
        var servForm=$("<div class='win_form'>"+
            			"<table class='win_table'><thead><tr>"+
                        "<td dateKey='path'>名称</td>"+
                        "<td dateKey='package'>java文件</td>"+
                        "<td dateKey='ctrl' bindfunc='setServOpt' class='last_child'>操作</td>"+
                        "</tr></thead><tbody></tbody></table></div>"+       			
        				"<div align=center style='margin-top:10px'><button class='btn' callFunc='click:createServFile' t='batch'>生成配置</button></div>");
    	                      
        var curId;
        var curUrl;
        var winTable=relaForm.find("table");      
        var beanTable=beanForm.find("table");
        var textTable=textForm.find("table");
        var paramDiv=$("<div style='width:600px'></div>");
        var beanDiv=$("<div style='width:600px'></div>");
        var commonParam={};         
    {% endblock %}
    {% block listhead %}
                   
		var listHeadCfg={
			name:{name:'名称',css:{width:'15%'},bindFunc:true},
			path:{name:'路径',css:{width:'15%'}},
			url:{name:'链接',css:{width:'15%'}},
			ctrl:{name:'操作',css:{width:'55%'},bindFunc:true}
		}
    	
		var renderTemplate=$("<div class='table_template'>"+
								"<div class='tr'>"+
										"<div class='td' jtext='name'></div>"+
										"<div class='td' jtext='path'></div>"+
										"<div class='td' jtext='url'></div>"+																							
										"<div class='td' jtext='ctrl'></div>"+			
								"</div>"+
							"</div>");
        function queryForm(cond){
        	$.getJSON("/do?action=htmlHandle.queryAll",cond,function(data){
				commonParam.form=data;
			});
			
        }
        function queryBean(cond){
        	$.getJSON("/do?action=table.queryAll",cond,function(data){
				
				commonParam.bean=data;
			});
        };
        function queryParam(){
        	$.postJSON("/do?action=project.qryParamDiv",null,function(data){
				paramDiv.html(data.content);
			});
        }
    {% endblock %}
    {% block listfunc %}
		tableHandle.addFunc("ctrl",function(){
			return $("<button class='btn' callFunc='click:editTemplate'>编辑</button>"+
            		"<button class='btn' callFunc='click:syncService'>同步服务</button>"+
                    "<button class='btn' callFunc='click:syncConfig'>参数配置</button>"+
                    "<button class='btn' callFunc='click:syncBean'>生成bean</button>"+ 
                    "<button class='btn' callFunc='click:syncText'>映射数据</button>"+                 
					"<button class='btn' callFunc='click:delTemplate'>删除</button>");
		},body);	
        tableHandle.addFunc("name",function(){
           return $("<a href='javascript:void(0)' callFunc='click:showRelaForm' >"+this.name+"</a>");
		},body);
        
        tableHandle.addSimpleFunc("setOpt",function(){
           return $("<a href='javascript:void(0)' callFunc='click:syncHtml' style='margin-right:20px;'>同步</a>"+
            		"<a href='javascript:void(0)' callFunc='click:delHtml'>删除</a>");
		});
		
		tableHandle.addSimpleFunc("setBeanOpt",function(){
           return $("<a href='javascript:void(0)' callFunc='click:createBean' style='margin-right:20px;'>同步</a>"+
            		"<a href='javascript:void(0)' callFunc='click:delHtml'>删除</a>");
		});		
        tableHandle.addSimpleFunc("setServOpt",function(){
           return $("<a href='javascript:void(0)' callFunc='click:createServ' style='margin-right:20px;'>生成java文件</a>");
		});    
		tableHandle.addSimpleFunc("setTextOpt",function(){
           return $("<a href='javascript:void(0)' callFunc='click:createDbBean' style='margin-right:20px;'>生成java文件</a>");
		});       		
	{% endblock %}	
	{% block load %}
        queryForm({page:1,pageSize:15,type:'html'});
        queryBean({page:1,pageSize:15});
        queryParam();
    {% endblock %}
	{% block other %}
       yanhx.addFunc("saveRela",function(){
           var arr=[];
           winTable.children("tbody").children("tr").each(function(){
               arr.push(tableHandle.doSimple({method:'$id',param:this}));     
           });
           if(arr.length){
           	   $.postJSON("/do?action=relaForm.saveRela",{id:curId,forms:JSON.stringify(arr)},function(){
	           		yanhx.exec("myWin.alert",{
           				content:"保存成功"          				
           			})
	           }); 
           }
       });
       yanhx.addFunc("saveBean",function(){
       	   var arr=[];
           beanTable.children("tbody").children("tr").each(function(){
               arr.push(tableHandle.doSimple({method:'$id',param:this}));     
           });
           if(arr.length){
           	   $.postJSON("/do?action=relaBean.saveRela",{id:curId,forms:JSON.stringify(arr)},function(){
	           		yanhx.exec("myWin.alert",{
           				content:"保存成功"
           			})
	           }); 
           }
       });
       yanhx.addFunc("syncText",function(){
       	   var rowData=body.$tableRow();
           yanhx.exec("myWin.open",{
           		elem:textForm,
           		title:'数据库映射',
           		model:true
           });
           curId=rowData.id;
           var url=rowData.url;
           curUrl=url;
           yanhx.exec("ajaxHandle.get",{
           		url:url,
           		callback:function(data){
           			textTable.table(data,true);
           		}
           });
       });	
       yanhx.addFunc("createDbBean",function(){
       	   var name=this.parent().prev().text();
       	   yanhx.exec("ajaxHandle.get",{
           		url:curUrl,
           		param:{name:name},
           		callback:function(data){
           			$.postJSON("/do?action=relaBean.syncDbBean",{id:curId,name:name,field:JSON.stringify(data)},function(ddd){
           				$.alert(ddd.message);
           			});
           		}
           });
       });
       yanhx.addFunc("syncJs",function(){
           $.postJSON("/do?action=project.syncJs",{id:curId},function(data){
           		yanhx.exec("myWin.alert",{
       				content:data.message
       			})
           }); 
       });
       yanhx.addFunc("syncHtmlJs",function(){
       	   var id=tableHandle.doSimple({method:'$id',param:this.parentUtil("tr")});
           $.postJSON("/do?action=project.syncHtmlJs",{id:id,proId:curId},function(data){
           		yanhx.exec("myWin.alert",{
       				content:data.message
       			})
           });
       });
       yanhx.addFunc("syncHtml",function(){
           var id=tableHandle.doSimple({method:'$id',param:this.parentUtil("tr")});
           $.postJSON("/do?action=project.syncHtml",{id:id,proId:curId},function(data){
           		yanhx.exec("myWin.alert",{
       				content:data.message
       			})
           }); 
       });  
       yanhx.addFunc("delHtml",function(){
           this.parents("tr").remove();
       }); 
       yanhx.addFunc("addBean",function(){
       	   var src=$src();
           var data=src.data("data");
           if(!data)
           		return;
           
           beanTable.table([data],false);
       });  
       yanhx.addFunc("addText",function(){
       	   var src=$src();
           var data=src.data("data");
           if(!data)
           		return;
           
           textTable.table([data],false);
       });                        
       yanhx.addFunc("addForm",function(){
           var src=$src();
           var data=src.data("data");
           if(!data)
           		return;
           
           winTable.table([data],false);
       });
       yanhx.addFunc("syncBean",function(){
       	   beanForm.find(".form_list").list(commonParam.bean,true);
       	   var rowData=body.$tableRow();
           yanhx.exec("myWin.open",{
           		elem:beanForm,
           		title:'项目bean',
           		model:true
           });
           yanhx.exec("qryProBean",rowData);
           curId=rowData.id;
       });
       yanhx.addFunc("qryProBean",function(rowData){
            curId=rowData.id;
        	$.postJSON("/do?action=relaBean.qryRela",{id:rowData.id},function(data){
           		beanTable.table(data,true);
           	});   
        });	
       yanhx.addFunc("createServFile",function(){
            $.postJSON("/do?action=service.servFile",{id:curId},function(data){
           		yanhx.exec("myWin.alert",{content:data.message});
           	});        	
       });
       yanhx.addFunc("createServ",function(){
       		var id=tableHandle.doSimple({method:'$id',param:this.parentUtil("tr")});
          	$.postJSON("/do?action=service.createServ",{id:curId,serId:id},function(data){
           		yanhx.exec("myWin.alert",{content:data.message});
           	}); 
       });
       yanhx.addFunc("syncService",function(){
            var rowData=body.$tableRow();
       		yanhx.exec("myWin.open",{
           		elem:servForm,
           		title:'服务配置',
           		model:true
            });
            curId=rowData.id;
            $.postJSON("/do?action=service.getServ",{id:rowData.id},function(data){
           		servForm.find("table").table(data,true);
           	}); 
       });
       yanhx.addFunc("createBean",function(){
       		var isBatch=this.attr("t")=='batch'?true:false;
       		var url='/do?action=relaBean.createFile';
       		var param={id:curId};
       		if(isBatch){
       			url='/do?action=relaBean.createBatchFile';
       		}
       		else{
       			param.beanId=tableHandle.doSimple({method:'$id',param:this.parentUtil("tr")});
       		}
       		$.postJSON(url,param,function(data){
           		yanhx.exec("myWin.alert",{content:data.message});
           	}); 
       });
       yanhx.addFunc("createFile",function(){
           var bean=paramDiv.getBean(false);
	       $.postJSON("/do?action=project.createFile",bean,function(data){
           		yanhx.exec("myWin.alert",{content:data.message});
           		yanhx.exec("myWin.closeWin");
           }); 
       });
       yanhx.addFunc("syncConfig",function(){
       	   var rowData=body.$tableRow();
           yanhx.exec("myWin.open",{
           		elem:paramDiv,
           		title:'参数配置',
           		model:true
           });
           $.postJSON("/do?action=proParam.qryProParam",{id:rowData.id},function(data){
           		paramDiv.fill(data);
           }); 
       });
     
       yanhx.addFunc("showRelaForm",function(){
           //项目绑定表单及页面同步
           relaForm.find(".form_list").list(commonParam.form,true);
           var rowData=body.$tableRow();
           yanhx.exec("myWin.open",{
           		elem:relaForm,
           		title:'表单发布',
           		model:true
           });
           yanhx.exec("qryRela",rowData);   
        });	
        yanhx.addFunc("qryRela",function(rowData){
            curId=rowData.id;
        	$.postJSON("/do?action=relaForm.qryRela",{id:rowData.id},function(data){
           		winTable.table(data,true);
           	});   
        });	
    {% endblock %}    		